package com.example.mybatispdemo.store.controller;


import com.example.mybatispdemo.store.entity.ProductEntity;
import com.example.mybatispdemo.store.entity.Product_supplierEntity;
import com.example.mybatispdemo.store.entity.SupplierEntity;
import com.example.mybatispdemo.store.exception.ProductException;
import com.example.mybatispdemo.store.service.IProductService;
import com.example.mybatispdemo.store.service.IProduct_supplierService;
import com.example.mybatispdemo.store.service.ISupplierService;
import com.example.mybatispdemo.store.service.impl.ProductServiceImpl;
import com.example.mybatispdemo.store.vo.ActionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljp
 * @since 2022-10-29
 */
@Api(tags="商品管理")
@RestController
@RequestMapping("/product")
public class ProductController {
    final
    IProductService productService;

    public ProductController(ProductServiceImpl productService) {
        this.productService = productService;
    }

    @Autowired
    ISupplierService supplierService;

    @Autowired
    IProduct_supplierService product_supplierService;

    @Transactional
    @ApiOperation("根据id查找商品")
    @GetMapping("/{id}")
    public ActionResult<ProductEntity> getProduct(@PathVariable@ApiParam("商品id") int id) throws ProductException {
        ProductEntity product = productService.getProductById(id);
        if(product==null){
            return ActionResult.fail("查找失败");
        }
        List<SupplierEntity>supplierEntities = new ArrayList<>();
        List<Product_supplierEntity>relation = product_supplierService.getByProductId(id);
        for(Product_supplierEntity entity:relation){
            SupplierEntity supplier = supplierService.getSupplierById(entity.getSupplierId());
            supplierEntities.add(supplier);
        }
        product.setSupplierList(supplierEntities);
        return ActionResult.success(product);
    }

    @Transactional
    @ApiOperation("根据条件查询商品")
    @GetMapping("/nameAQuantity")
    public ActionResult<ProductEntity> findProduct(@ApiParam("商品名称")String name,@ApiParam("商品数量")float quantity) throws ProductException{
        List<ProductEntity> productEntityList=productService.getProuctByNameQuantity(name,quantity);
        if(productEntityList!=null){
            for(ProductEntity product:productEntityList){
                int id = product.getId();
                List<SupplierEntity>supplierEntities = new ArrayList<>();
                List<Product_supplierEntity>relation = product_supplierService.getByProductId(id);
                for(Product_supplierEntity entity:relation){
                    SupplierEntity supplier = supplierService.getSupplierById(entity.getSupplierId());
                    supplierEntities.add(supplier);
                }
                product.setSupplierList(supplierEntities);
            }
        }
        return ActionResult.success(productEntityList);
    }

    @Transactional
    @ApiOperation("添加商品")
    @PostMapping
    public ActionResult<ProductEntity> addOneProduct(@RequestBody ProductEntity product)throws ProductException{
        if(productService.addProduct(product)){
            ProductEntity product1 = productService.getProductByName(product.getName()).get(0);
            List<SupplierEntity>supplierEntityList = product.getSupplierList();

            Product_supplierEntity product_supplierEntity = new Product_supplierEntity();

            for(SupplierEntity entity:supplierEntityList){
                if(supplierService.getSupplierById(entity.getId())==null){
                    return ActionResult.fail("创建失败！供销商不存在");
                }
                //创建关联信息
                 product_supplierEntity = product_supplierService.addItem(product1.getId(),entity.getId());
                if(product_supplierEntity==null){
                    return ActionResult.fail("创建失败！");
                }
            }
            return ActionResult.success("创建成功！");
        }else{
            return ActionResult.fail("创建失败！产品已存在");
        }
    }

    @Transactional
    @ApiOperation("更新商品信息")
    @DeleteMapping()
    public ActionResult<ProductEntity> updateProduct(@PathVariable int id,@RequestBody ProductEntity product){
        if(productService.updateProductById(id,product)){
            List<SupplierEntity> supplierEntityList = product.getSupplierList();
            for(SupplierEntity supplier:supplierEntityList){
                //查看关联信息是否需要添加
                if(supplierService.getSupplierById(supplier.getId())==null){
                    return ActionResult.fail("更新失败！");
                }
                if(product_supplierService.getEntity(product.getId(),supplier.getId())!=null){
                    product_supplierService.addItem(product.getId(),supplier.getId());
                }
            }
            return ActionResult.success("更新成功！");
        }else{
            return ActionResult.fail("更新失败！");
        }
    }

    @Transactional
    @ApiOperation("更新商品信息")
    @PutMapping()
    public ActionResult<ProductEntity> deleteProduct(@PathVariable int id){
        if(productService.deleteProductById(id)){
            //将关联信息删除
            product_supplierService.deleteByProductId(id);
            return ActionResult.success("更新成功！");
        }else{
            return ActionResult.fail("更新失败！");
        }
    }
}

